﻿using System;
using System.Collections.Generic;
using System.Text;
using DevScope.SharePoint.Utils.BulkDelete.Helpers.Caml;
using DevScope.SharePoint.Utils.BulkDelete.Helpers;
using DevScope.SharePoint.Utils.BulkDelete.Helpers.Caml.Enumerations;
using System.Data;
using DevScope.SharePoint.Utils.BulkDelete.Logging;

namespace DevScope.SharePoint.Utils.BulkDelete
{
    public static class BulkDelete
    {
        /// <summary>
        /// Gets the list items.
        /// </summary>
        /// <param name="webUrl">The web URL.</param>
        /// <param name="listName">Name of the list.</param>
        /// <param name="viewFields">The view fields.</param>
        /// <param name="query">The query.</param>
        /// <param name="queryOptions">The query options.</param>
        /// <param name="rowLimit">The row limit.</param>
        /// <returns></returns>
        public static DataTable GetListItems(string webUrl, string listName, string viewFields, string query, string queryOptions, int rowLimit)
        {
            if (string.IsNullOrEmpty(webUrl))
                throw new ArgumentNullException("webUrl");
            if (string.IsNullOrEmpty(listName))
                throw new ArgumentNullException("listName");

            CamlBuilder batchBuilder = new CamlBuilder(CamlTypeEnum.GetListItems);

            if (!string.IsNullOrEmpty(viewFields))
                batchBuilder.ViewFieldsNode.InnerXml = viewFields;
            else
                batchBuilder.ViewFieldsNode.InnerXml = Constants.CAML.ViewFields;

            if (!string.IsNullOrEmpty(query))
                batchBuilder.QueryNode.InnerXml = query;

            if (!string.IsNullOrEmpty(queryOptions))
                batchBuilder.QueryOptionsNode.InnerXml = queryOptions;

            return SharePointHelper.QueryList(webUrl, listName, rowLimit
                                                    , batchBuilder.QueryNode
                                                    , batchBuilder.ViewFieldsNode
                                                    , batchBuilder.QueryOptionsNode);
        }

        /// <summary>
        /// Deletes the list items.
        /// </summary>
        /// <param name="dtItems">The dt items.</param>
        /// <param name="webUrl">The web URL.</param>
        /// <param name="listName">Name of the list.</param>
        public static void DeleteListItems(DataTable dtItems, string webUrl, string listName)
        {
            if (dtItems == null)
                throw new ArgumentNullException("dtItems");
            if (string.IsNullOrEmpty(webUrl))
                throw new ArgumentNullException("webUrl");
            if (string.IsNullOrEmpty(listName))
                throw new ArgumentNullException("listName");

            CamlBuilder builder = new CamlBuilder(CamlTypeEnum.UpdateListItems);

            builder.UpdateBatchNode("OnError", CamlOnErrorTypeEnum.Return.ToString());

            int methodNumber = 1;

            foreach (DataRow dr in dtItems.Rows)
            {
                builder.AddMethodNode(methodNumber, CamlCommandTypeEnum.Delete);

                if (!dr.Table.Columns.Contains(Constants.CAML.ResultColumns.Id))
                    throw new ApplicationException(string.Format("Column '{0}' must exist in the result.", Constants.CAML.ResultColumns.Id));

                if (!dr.Table.Columns.Contains(Constants.CAML.ResultColumns.EncodedAbsUrl))
                    throw new ApplicationException(string.Format("Column '{0}' must exist in the result.", Constants.CAML.ResultColumns.EncodedAbsUrl));

                string id = dr[Constants.CAML.ResultColumns.Id].ToString();
                builder.AddFieldNode(methodNumber, Constants.CAML.BatchColumns.Id, id);

                string encodedAbsUrl = dr[Constants.CAML.ResultColumns.EncodedAbsUrl].ToString();
                builder.AddFieldNode(methodNumber, Constants.CAML.BatchColumns.FileRef, encodedAbsUrl);

                methodNumber++;
            }

            Logger.Debug("Update batch xml: '{0}'", builder.BatchNode.OuterXml);

            SharePointHelper.UpdateList(webUrl, listName, builder.BatchNode);
        }
    }
}
